<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>set_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_structs_set_test.js.html">set_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2006 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.structs.SetTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.structs.SetTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.iter&#39;);
<a name="line19"></a>goog.require(&#39;goog.structs&#39;);
<a name="line20"></a>goog.require(&#39;goog.structs.Set&#39;);
<a name="line21"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line22"></a>
<a name="line23"></a>var Set = goog.structs.Set;
<a name="line24"></a>
<a name="line25"></a>function stringifySet(s) {
<a name="line26"></a>  return goog.structs.getValues(s).join(&#39;&#39;);
<a name="line27"></a>}
<a name="line28"></a>
<a name="line29"></a>function testGetCount() {
<a name="line30"></a>  var s = new Set;
<a name="line31"></a>  var a = new String(&#39;a&#39;); s.add(a);
<a name="line32"></a>  var b = new String(&#39;b&#39;); s.add(b);
<a name="line33"></a>  var c = new String(&#39;c&#39;); s.add(c);
<a name="line34"></a>  assertEquals(&#39;count, should be 3&#39;, s.getCount(), 3);
<a name="line35"></a>  var d = new String(&#39;d&#39;); s.add(d);
<a name="line36"></a>  assertEquals(&#39;count, should be 4&#39;, s.getCount(), 4);
<a name="line37"></a>  s.remove(d);
<a name="line38"></a>  assertEquals(&#39;count, should be 3&#39;, s.getCount(), 3);
<a name="line39"></a>
<a name="line40"></a>  s = new Set;
<a name="line41"></a>  s.add(&#39;a&#39;);
<a name="line42"></a>  s.add(&#39;b&#39;);
<a name="line43"></a>  s.add(&#39;c&#39;);
<a name="line44"></a>  assertEquals(&#39;count, should be 3&#39;, s.getCount(), 3);
<a name="line45"></a>  s.add(&#39;d&#39;);
<a name="line46"></a>  assertEquals(&#39;count, should be 4&#39;, s.getCount(), 4);
<a name="line47"></a>  s.remove(&#39;d&#39;);
<a name="line48"></a>  assertEquals(&#39;count, should be 3&#39;, s.getCount(), 3);
<a name="line49"></a>}
<a name="line50"></a>
<a name="line51"></a>function testGetValues() {
<a name="line52"></a>  var s = new Set;
<a name="line53"></a>  var a = new String(&#39;a&#39;); s.add(a);
<a name="line54"></a>  var b = new String(&#39;b&#39;); s.add(b);
<a name="line55"></a>  var c = new String(&#39;c&#39;); s.add(c);
<a name="line56"></a>  var d = new String(&#39;d&#39;); s.add(d);
<a name="line57"></a>  assertEquals(s.getValues().join(&#39;&#39;), &#39;abcd&#39;);
<a name="line58"></a>
<a name="line59"></a>  var s = new Set;
<a name="line60"></a>  s.add(&#39;a&#39;);
<a name="line61"></a>  s.add(&#39;b&#39;);
<a name="line62"></a>  s.add(&#39;c&#39;);
<a name="line63"></a>  s.add(&#39;d&#39;);
<a name="line64"></a>  assertEquals(s.getValues().join(&#39;&#39;), &#39;abcd&#39;);
<a name="line65"></a>}
<a name="line66"></a>
<a name="line67"></a>function testContains() {
<a name="line68"></a>  var s = new Set;
<a name="line69"></a>  var a = new String(&#39;a&#39;); s.add(a);
<a name="line70"></a>  var b = new String(&#39;b&#39;); s.add(b);
<a name="line71"></a>  var c = new String(&#39;c&#39;); s.add(c);
<a name="line72"></a>  var d = new String(&#39;d&#39;); s.add(d);
<a name="line73"></a>  var e = new String(&#39;e&#39;);
<a name="line74"></a>
<a name="line75"></a>  assertTrue(&quot;contains, Should contain &#39;a&#39;&quot;, s.contains(a));
<a name="line76"></a>  assertFalse(&quot;contains, Should not contain &#39;e&#39;&quot;, s.contains(e));
<a name="line77"></a>
<a name="line78"></a>  s = new Set;
<a name="line79"></a>  s.add(&#39;a&#39;);
<a name="line80"></a>  s.add(&#39;b&#39;);
<a name="line81"></a>  s.add(&#39;c&#39;);
<a name="line82"></a>  s.add(&#39;d&#39;);
<a name="line83"></a>
<a name="line84"></a>  assertTrue(&quot;contains, Should contain &#39;a&#39;&quot;, s.contains(&#39;a&#39;));
<a name="line85"></a>  assertFalse(&quot;contains, Should not contain &#39;e&#39;&quot;, s.contains(&#39;e&#39;));
<a name="line86"></a>}
<a name="line87"></a>
<a name="line88"></a>function testContainsFunctionValue() {
<a name="line89"></a>  var s = new Set;
<a name="line90"></a>
<a name="line91"></a>  var fn1 = function() {};
<a name="line92"></a>
<a name="line93"></a>  assertFalse(s.contains(fn1));
<a name="line94"></a>  s.add(fn1);
<a name="line95"></a>  assertTrue(s.contains(fn1));
<a name="line96"></a>
<a name="line97"></a>  var fn2 = function() {};
<a name="line98"></a>
<a name="line99"></a>  assertFalse(s.contains(fn2));
<a name="line100"></a>  s.add(fn2);
<a name="line101"></a>  assertTrue(s.contains(fn2));
<a name="line102"></a>
<a name="line103"></a>  assertEquals(s.getCount(), 2);
<a name="line104"></a>}
<a name="line105"></a>
<a name="line106"></a>function testContainsAll() {
<a name="line107"></a>  var set = new Set([1, 2, 3]);
<a name="line108"></a>
<a name="line109"></a>  assertTrue(&#39;{1, 2, 3} contains []&#39;, set.containsAll([]));
<a name="line110"></a>  assertTrue(&#39;{1, 2, 3} contains [1]&#39;, set.containsAll([1]));
<a name="line111"></a>  assertTrue(&#39;{1, 2, 3} contains [1, 1]&#39;, set.containsAll([1, 1]));
<a name="line112"></a>  assertTrue(&#39;{1, 2, 3} contains [3, 2, 1]&#39;, set.containsAll([3, 2, 1]));
<a name="line113"></a>  assertFalse(&quot;{1, 2, 3} doesn&#39;t contain [4]&quot;, set.containsAll([4]));
<a name="line114"></a>  assertFalse(&quot;{1, 2, 3} doesn&#39;t contain [1, 4]&quot;, set.containsAll([1, 4]));
<a name="line115"></a>
<a name="line116"></a>  assertTrue(&#39;{1, 2, 3} contains {a: 1}&#39;, set.containsAll({a: 1}));
<a name="line117"></a>  assertFalse(&quot;{1, 2, 3} doesn&#39;t contain {a: 4}&quot;, set.containsAll({a: 4}));
<a name="line118"></a>
<a name="line119"></a>  assertTrue(&#39;{1, 2, 3} contains {1}&#39;, set.containsAll(new Set([1])));
<a name="line120"></a>  assertFalse(&quot;{1, 2, 3} doesn&#39;t contain {4}&quot;, set.containsAll(new Set([4])));
<a name="line121"></a>}
<a name="line122"></a>
<a name="line123"></a>function testIntersection() {
<a name="line124"></a>  var emptySet = new Set;
<a name="line125"></a>
<a name="line126"></a>  assertTrue(&#39;intersection of empty set and [] should be empty&#39;,
<a name="line127"></a>      emptySet.intersection([]).isEmpty());
<a name="line128"></a>  assertIntersection(&#39;intersection of 2 empty sets should be empty&#39;,
<a name="line129"></a>      emptySet, new Set(), new Set());
<a name="line130"></a>
<a name="line131"></a>  var abcdSet = new Set();
<a name="line132"></a>  abcdSet.add(&#39;a&#39;);
<a name="line133"></a>  abcdSet.add(&#39;b&#39;);
<a name="line134"></a>  abcdSet.add(&#39;c&#39;);
<a name="line135"></a>  abcdSet.add(&#39;d&#39;);
<a name="line136"></a>
<a name="line137"></a>  assertTrue(&#39;intersection of populated set and [] should be empty&#39;,
<a name="line138"></a>      abcdSet.intersection([]).isEmpty());
<a name="line139"></a>  assertIntersection(
<a name="line140"></a>      &#39;intersection of populated set and empty set should be empty&#39;,
<a name="line141"></a>      abcdSet, new Set(), new Set());
<a name="line142"></a>
<a name="line143"></a>  var bcSet = new Set([&#39;b&#39;, &#39;c&#39;]);
<a name="line144"></a>  assertIntersection(&#39;intersection of [a,b,c,d] and [b,c]&#39;,
<a name="line145"></a>      abcdSet, bcSet, bcSet);
<a name="line146"></a>
<a name="line147"></a>  var bceSet = new Set([&#39;b&#39;, &#39;c&#39;, &#39;e&#39;]);
<a name="line148"></a>  assertIntersection(&#39;intersection of [a,b,c,d] and [b,c,e]&#39;,
<a name="line149"></a>      abcdSet, bceSet, bcSet);
<a name="line150"></a>}
<a name="line151"></a>
<a name="line152"></a>function testDifference() {
<a name="line153"></a>  var emptySet = new Set;
<a name="line154"></a>
<a name="line155"></a>  assertTrue(&#39;difference of empty set and [] should be empty&#39;,
<a name="line156"></a>      emptySet.difference([]).isEmpty());
<a name="line157"></a>  assertTrue(&#39;difference of 2 empty sets should be empty&#39;,
<a name="line158"></a>      emptySet.difference(new Set()).isEmpty());
<a name="line159"></a>
<a name="line160"></a>  var abcdSet = new Set([&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;]);
<a name="line161"></a>
<a name="line162"></a>  assertTrue(&#39;difference of populated set and [] should be the populated set&#39;,
<a name="line163"></a>      abcdSet.difference([]).equals(abcdSet));
<a name="line164"></a>  assertTrue(
<a name="line165"></a>      &#39;difference of populated set and empty set should be the populated set&#39;,
<a name="line166"></a>      abcdSet.difference(new Set()).equals(abcdSet));
<a name="line167"></a>  assertTrue(&#39;difference of two identical sets should be the empty set&#39;,
<a name="line168"></a>      abcdSet.difference(abcdSet).equals(new Set()));
<a name="line169"></a>
<a name="line170"></a>  var bcSet = new Set([&#39;b&#39;, &#39;c&#39;]);
<a name="line171"></a>  assertTrue(&#39;difference of [a,b,c,d] and [b,c] shoule be [a,d]&#39;,
<a name="line172"></a>      abcdSet.difference(bcSet).equals(new Set([&#39;a&#39;, &#39;d&#39;])));
<a name="line173"></a>  assertTrue(&#39;difference of [b,c] and [a,b,c,d] should be the empty set&#39;,
<a name="line174"></a>      bcSet.difference(abcdSet).equals(new Set()));
<a name="line175"></a>
<a name="line176"></a>  var xyzSet = new Set([&#39;x&#39;, &#39;y&#39;, &#39;z&#39;]);
<a name="line177"></a>  assertTrue(&#39;difference of [a,b,c,d] and [x,y,z] should be the [a,b,c,d]&#39;,
<a name="line178"></a>      abcdSet.difference(xyzSet).equals(abcdSet));
<a name="line179"></a>}
<a name="line180"></a>
<a name="line181"></a>
<a name="line182"></a>/**
<a name="line183"></a> * Helper function to assert intersection is commutative.
<a name="line184"></a> */
<a name="line185"></a>function assertIntersection(msg, set1, set2, expectedIntersection) {
<a name="line186"></a>  assertTrue(msg + &#39;: set1-&gt;set2&#39;,
<a name="line187"></a>      set1.intersection(set2).equals(expectedIntersection));
<a name="line188"></a>  assertTrue(msg + &#39;: set2-&gt;set1&#39;,
<a name="line189"></a>      set2.intersection(set1).equals(expectedIntersection));
<a name="line190"></a>}
<a name="line191"></a>
<a name="line192"></a>function testRemoveAll() {
<a name="line193"></a>  assertRemoveAll(&#39;removeAll of empty set from empty set&#39;, [], [], []);
<a name="line194"></a>  assertRemoveAll(&#39;removeAll of empty set from populated set&#39;,
<a name="line195"></a>      [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;], [], [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;]);
<a name="line196"></a>  assertRemoveAll(&#39;removeAll of [a,d] from [a,b,c,d]&#39;,
<a name="line197"></a>      [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;], [&#39;a&#39;, &#39;d&#39;], [&#39;b&#39;, &#39;c&#39;]);
<a name="line198"></a>  assertRemoveAll(&#39;removeAll of [b,c] from [a,b,c,d]&#39;,
<a name="line199"></a>      [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;], [&#39;b&#39;, &#39;c&#39;], [&#39;a&#39;, &#39;d&#39;]);
<a name="line200"></a>  assertRemoveAll(&#39;removeAll of [b,c,e] from [a,b,c,d]&#39;,
<a name="line201"></a>      [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;], [&#39;b&#39;, &#39;c&#39;, &#39;e&#39;], [&#39;a&#39;, &#39;d&#39;]);
<a name="line202"></a>  assertRemoveAll(&#39;removeAll of [a,b,c,d] from [a,d]&#39;,
<a name="line203"></a>      [&#39;a&#39;, &#39;d&#39;], [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;], []);
<a name="line204"></a>  assertRemoveAll(&#39;removeAll of [a,b,c,d] from [b,c]&#39;,
<a name="line205"></a>      [&#39;b&#39;, &#39;c&#39;], [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;], []);
<a name="line206"></a>  assertRemoveAll(&#39;removeAll of [a,b,c,d] from [b,c,e]&#39;,
<a name="line207"></a>      [&#39;b&#39;, &#39;c&#39;, &#39;e&#39;], [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;], [&#39;e&#39;]);
<a name="line208"></a>}
<a name="line209"></a>
<a name="line210"></a>
<a name="line211"></a>/**
<a name="line212"></a> * Helper function to test removeAll.
<a name="line213"></a> */
<a name="line214"></a>function assertRemoveAll(msg, elements1, elements2, expectedResult) {
<a name="line215"></a>  var set1 = new Set(elements1);
<a name="line216"></a>  var set2 = new Set(elements2);
<a name="line217"></a>  set1.removeAll(set2);
<a name="line218"></a>
<a name="line219"></a>  assertTrue(msg + &#39;: set1 count increased after removeAll&#39;,
<a name="line220"></a>      elements1.length &gt;= set1.getCount());
<a name="line221"></a>  assertEquals(msg + &#39;: set2 count changed after removeAll&#39;,
<a name="line222"></a>      elements2.length, set2.getCount());
<a name="line223"></a>  assertTrue(msg + &#39;: wrong set1 after removeAll&#39;, set1.equals(expectedResult));
<a name="line224"></a>  assertIntersection(msg + &#39;: non-empty intersection after removeAll&#39;,
<a name="line225"></a>      set1, set2, []);
<a name="line226"></a>}
<a name="line227"></a>
<a name="line228"></a>function testAdd() {
<a name="line229"></a>  var s = new Set;
<a name="line230"></a>  var a = new String(&#39;a&#39;);
<a name="line231"></a>  var b = new String(&#39;b&#39;);
<a name="line232"></a>  s.add(a);
<a name="line233"></a>  assertTrue(s.contains(a));
<a name="line234"></a>  s.add(b);
<a name="line235"></a>  assertTrue(s.contains(b));
<a name="line236"></a>
<a name="line237"></a>  s = new Set;
<a name="line238"></a>  s.add(&#39;a&#39;);
<a name="line239"></a>  assertTrue(s.contains(&#39;a&#39;));
<a name="line240"></a>  s.add(&#39;b&#39;);
<a name="line241"></a>  assertTrue(s.contains(&#39;b&#39;));
<a name="line242"></a>  s.add(null);
<a name="line243"></a>  assertTrue(&#39;contains null&#39;, s.contains(null));
<a name="line244"></a>  assertFalse(&#39;does not contain &quot;null&quot;&#39;, s.contains(&#39;null&#39;));
<a name="line245"></a>}
<a name="line246"></a>
<a name="line247"></a>
<a name="line248"></a>function testClear() {
<a name="line249"></a>  var s = new Set;
<a name="line250"></a>  var a = new String(&#39;a&#39;); s.add(a);
<a name="line251"></a>  var b = new String(&#39;b&#39;); s.add(b);
<a name="line252"></a>  var c = new String(&#39;c&#39;); s.add(c);
<a name="line253"></a>  var d = new String(&#39;d&#39;); s.add(d);
<a name="line254"></a>  s.clear();
<a name="line255"></a>  assertTrue(&#39;cleared so it should be empty&#39;, s.isEmpty());
<a name="line256"></a>  assertTrue(&quot;cleared so it should not contain &#39;a&#39; key&quot;, !s.contains(a));
<a name="line257"></a>
<a name="line258"></a>  s = new Set;
<a name="line259"></a>  s.add(&#39;a&#39;);
<a name="line260"></a>  s.add(&#39;b&#39;);
<a name="line261"></a>  s.add(&#39;c&#39;);
<a name="line262"></a>  s.add(&#39;d&#39;);
<a name="line263"></a>  s.clear();
<a name="line264"></a>  assertTrue(&#39;cleared so it should be empty&#39;, s.isEmpty());
<a name="line265"></a>  assertTrue(&quot;cleared so it should not contain &#39;a&#39; key&quot;, !s.contains(&#39;a&#39;));
<a name="line266"></a>}
<a name="line267"></a>
<a name="line268"></a>
<a name="line269"></a>function testAddAll() {
<a name="line270"></a>  var s = new Set;
<a name="line271"></a>  var a = new String(&#39;a&#39;);
<a name="line272"></a>  var b = new String(&#39;b&#39;);
<a name="line273"></a>  var c = new String(&#39;c&#39;);
<a name="line274"></a>  var d = new String(&#39;d&#39;);
<a name="line275"></a>  s.addAll([a, b, c, d]);
<a name="line276"></a>  assertTrue(&#39;addAll so it should not be empty&#39;, !s.isEmpty());
<a name="line277"></a>  assertTrue(&quot;addAll so it should contain &#39;c&#39; key&quot;, s.contains(c));
<a name="line278"></a>
<a name="line279"></a>  var s2 = new Set;
<a name="line280"></a>  s2.addAll(s);
<a name="line281"></a>  assertTrue(&#39;addAll so it should not be empty&#39;, !s2.isEmpty());
<a name="line282"></a>  assertTrue(&quot;addAll so it should contain &#39;c&#39; key&quot;, s2.contains(c));
<a name="line283"></a>
<a name="line284"></a>
<a name="line285"></a>  s = new Set;
<a name="line286"></a>  s.addAll([&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;]);
<a name="line287"></a>  assertTrue(&#39;addAll so it should not be empty&#39;, !s.isEmpty());
<a name="line288"></a>  assertTrue(&quot;addAll so it should contain &#39;c&#39; key&quot;, s.contains(&#39;c&#39;));
<a name="line289"></a>
<a name="line290"></a>  s2 = new Set;
<a name="line291"></a>  s2.addAll(s);
<a name="line292"></a>  assertTrue(&#39;addAll so it should not be empty&#39;, !s2.isEmpty());
<a name="line293"></a>  assertTrue(&quot;addAll so it should contain &#39;c&#39; key&quot;, s2.contains(&#39;c&#39;));
<a name="line294"></a>}
<a name="line295"></a>
<a name="line296"></a>
<a name="line297"></a>function testConstructor() {
<a name="line298"></a>  var s = new Set;
<a name="line299"></a>  var a = new String(&#39;a&#39;); s.add(a);
<a name="line300"></a>  var b = new String(&#39;b&#39;); s.add(b);
<a name="line301"></a>  var c = new String(&#39;c&#39;); s.add(c);
<a name="line302"></a>  var d = new String(&#39;d&#39;); s.add(d);
<a name="line303"></a>  var s2 = new Set(s);
<a name="line304"></a>  assertFalse(&#39;constr with Set so it should not be empty&#39;, s2.isEmpty());
<a name="line305"></a>  assertTrue(&#39;constr with Set so it should contain c&#39;, s2.contains(c));
<a name="line306"></a>
<a name="line307"></a>  s = new Set;
<a name="line308"></a>  s.add(&#39;a&#39;);
<a name="line309"></a>  s.add(&#39;b&#39;);
<a name="line310"></a>  s.add(&#39;c&#39;);
<a name="line311"></a>  s.add(&#39;d&#39;);
<a name="line312"></a>  s2 = new Set(s);
<a name="line313"></a>  assertFalse(&#39;constr with Set so it should not be empty&#39;, s2.isEmpty());
<a name="line314"></a>  assertTrue(&#39;constr with Set so it should contain c&#39;, s2.contains(&#39;c&#39;));
<a name="line315"></a>}
<a name="line316"></a>
<a name="line317"></a>
<a name="line318"></a>function testClone() {
<a name="line319"></a>  var s = new Set;
<a name="line320"></a>  var a = new String(&#39;a&#39;); s.add(a);
<a name="line321"></a>  var b = new String(&#39;b&#39;); s.add(b);
<a name="line322"></a>  var c = new String(&#39;c&#39;); s.add(c);
<a name="line323"></a>  var d = new String(&#39;d&#39;); s.add(d);
<a name="line324"></a>
<a name="line325"></a>  var s2 = s.clone();
<a name="line326"></a>  assertFalse(&#39;clone so it should not be empty&#39;, s2.isEmpty());
<a name="line327"></a>  assertTrue(&quot;clone so it should contain &#39;c&#39; key&quot;, s2.contains(c));
<a name="line328"></a>
<a name="line329"></a>  var s = new Set;
<a name="line330"></a>  s.add(&#39;a&#39;);
<a name="line331"></a>  s.add(&#39;b&#39;);
<a name="line332"></a>  s.add(&#39;c&#39;);
<a name="line333"></a>  s.add(&#39;d&#39;);
<a name="line334"></a>
<a name="line335"></a>  s2 = s.clone();
<a name="line336"></a>  assertFalse(&#39;clone so it should not be empty&#39;, s2.isEmpty());
<a name="line337"></a>  assertTrue(&quot;clone so it should contain &#39;c&#39; key&quot;, s2.contains(&#39;c&#39;));
<a name="line338"></a>}
<a name="line339"></a>
<a name="line340"></a>
<a name="line341"></a>/**
<a name="line342"></a> * Helper method for testEquals().
<a name="line343"></a> * @param {Object} a First element to use in the tests.
<a name="line344"></a> * @param {Object} b Second element to use in the tests.
<a name="line345"></a> * @param {Object} c Third element to use in the tests.
<a name="line346"></a> * @param {Object} d Fourth element to use in the tests.
<a name="line347"></a> */
<a name="line348"></a>function helperForTestEquals(a, b, c, d) {
<a name="line349"></a>  var s = new Set([a, b, c]);
<a name="line350"></a>
<a name="line351"></a>  assertTrue(&#39;set == itself&#39;, s.equals(s));
<a name="line352"></a>  assertTrue(&#39;set == same set&#39;, s.equals(new Set([a, b, c])));
<a name="line353"></a>  assertTrue(&#39;set == its clone&#39;, s.equals(s.clone()));
<a name="line354"></a>  assertTrue(&#39;set == array of same elements&#39;, s.equals([a, b, c]));
<a name="line355"></a>  assertTrue(&#39;set == array of same elements in different order&#39;,
<a name="line356"></a>             s.equals([c, b, a]));
<a name="line357"></a>
<a name="line358"></a>  assertFalse(&#39;set != empty set&#39;, s.equals(new Set));
<a name="line359"></a>  assertFalse(&#39;set != its subset&#39;, s.equals(new Set([a, c])));
<a name="line360"></a>  assertFalse(&#39;set != its superset&#39;,
<a name="line361"></a>              s.equals(new Set([a, b, c, d])));
<a name="line362"></a>  assertFalse(&#39;set != different set&#39;,
<a name="line363"></a>              s.equals(new Set([b, c, d])));
<a name="line364"></a>  assertFalse(&#39;set != its subset as array&#39;, s.equals([a, c]));
<a name="line365"></a>  assertFalse(&#39;set != its superset as array&#39;, s.equals([a, b, c, d]));
<a name="line366"></a>  assertFalse(&#39;set != different set as array&#39;, s.equals([b, c, d]));
<a name="line367"></a>  assertFalse(&#39;set != [a, b, c, c]&#39;, s.equals([a, b, c, c]));
<a name="line368"></a>  assertFalse(&#39;set != [a, b, b]&#39;, s.equals([a, b, b]));
<a name="line369"></a>  assertFalse(&#39;set != [a, a]&#39;, s.equals([a, a]));
<a name="line370"></a>}
<a name="line371"></a>
<a name="line372"></a>
<a name="line373"></a>function testEquals() {
<a name="line374"></a>  helperForTestEquals(1, 2, 3, 4);
<a name="line375"></a>  helperForTestEquals(&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;);
<a name="line376"></a>  helperForTestEquals(new String(&#39;a&#39;), new String(&#39;b&#39;),
<a name="line377"></a>                      new String(&#39;c&#39;), new String(&#39;d&#39;));
<a name="line378"></a>}
<a name="line379"></a>
<a name="line380"></a>
<a name="line381"></a>/**
<a name="line382"></a> * Helper method for testIsSubsetOf().
<a name="line383"></a> * @param {Object} a First element to use in the tests.
<a name="line384"></a> * @param {Object} b Second element to use in the tests.
<a name="line385"></a> * @param {Object} c Third element to use in the tests.
<a name="line386"></a> * @param {Object} d Fourth element to use in the tests.
<a name="line387"></a> */
<a name="line388"></a>function helperForTestIsSubsetOf(a, b, c, d) {
<a name="line389"></a>  var s = new Set([a, b, c]);
<a name="line390"></a>
<a name="line391"></a>  assertTrue(&#39;set &lt;= itself&#39;, s.isSubsetOf(s));
<a name="line392"></a>  assertTrue(&#39;set &lt;= same set&#39;,
<a name="line393"></a>             s.isSubsetOf(new Set([a, b, c])));
<a name="line394"></a>  assertTrue(&#39;set &lt;= its clone&#39;, s.isSubsetOf(s.clone()));
<a name="line395"></a>  assertTrue(&#39;set &lt;= array of same elements&#39;, s.isSubsetOf([a, b, c]));
<a name="line396"></a>  assertTrue(&#39;set &lt;= array of same elements in different order&#39;,
<a name="line397"></a>             s.equals([c, b, a]));
<a name="line398"></a>
<a name="line399"></a>  assertTrue(&#39;set &lt;= Set([a, b, c, d])&#39;,
<a name="line400"></a>             s.isSubsetOf(new Set([a, b, c, d])));
<a name="line401"></a>  assertTrue(&#39;set &lt;= [a, b, c, d]&#39;, s.isSubsetOf([a, b, c, d]));
<a name="line402"></a>  assertTrue(&#39;set &lt;= [a, b, c, c]&#39;, s.isSubsetOf([a, b, c, c]));
<a name="line403"></a>
<a name="line404"></a>  assertFalse(&#39;set !&lt;= Set([a, b])&#39;,
<a name="line405"></a>              s.isSubsetOf(new Set([a, b])));
<a name="line406"></a>  assertFalse(&#39;set !&lt;= [a, b]&#39;, s.isSubsetOf([a, b]));
<a name="line407"></a>  assertFalse(&#39;set !&lt;= Set([c, d])&#39;,
<a name="line408"></a>              s.isSubsetOf(new Set([c, d])));
<a name="line409"></a>  assertFalse(&#39;set !&lt;= [c, d]&#39;, s.isSubsetOf([c, d]));
<a name="line410"></a>  assertFalse(&#39;set !&lt;= Set([a, c, d])&#39;,
<a name="line411"></a>              s.isSubsetOf(new Set([a, c, d])));
<a name="line412"></a>  assertFalse(&#39;set !&lt;= [a, c, d]&#39;, s.isSubsetOf([a, c, d]));
<a name="line413"></a>  assertFalse(&#39;set !&lt;= [a, a, b]&#39;, s.isSubsetOf([a, a, b]));
<a name="line414"></a>  assertFalse(&#39;set !&lt;= [a, a, b, b]&#39;, s.isSubsetOf([a, a, b, b]));
<a name="line415"></a>}
<a name="line416"></a>
<a name="line417"></a>
<a name="line418"></a>function testIsSubsetOf() {
<a name="line419"></a>  helperForTestIsSubsetOf(1, 2, 3, 4);
<a name="line420"></a>  helperForTestIsSubsetOf(&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;);
<a name="line421"></a>  helperForTestIsSubsetOf(new String(&#39;a&#39;), new String(&#39;b&#39;),
<a name="line422"></a>                          new String(&#39;c&#39;), new String(&#39;d&#39;));
<a name="line423"></a>}
<a name="line424"></a>
<a name="line425"></a>
<a name="line426"></a>function testForEach() {
<a name="line427"></a>  var s = new Set;
<a name="line428"></a>  var a = new String(&#39;a&#39;); s.add(a);
<a name="line429"></a>  var b = new String(&#39;b&#39;); s.add(b);
<a name="line430"></a>  var c = new String(&#39;c&#39;); s.add(c);
<a name="line431"></a>  var d = new String(&#39;d&#39;); s.add(d);
<a name="line432"></a>  var str = &#39;&#39;;
<a name="line433"></a>  goog.structs.forEach(s, function(val, key, set) {
<a name="line434"></a>    assertUndefined(key);
<a name="line435"></a>    assertEquals(s, set);
<a name="line436"></a>    str += val;
<a name="line437"></a>  });
<a name="line438"></a>  assertEquals(str, &#39;abcd&#39;);
<a name="line439"></a>
<a name="line440"></a>  s = new Set;
<a name="line441"></a>  s.add(&#39;a&#39;);
<a name="line442"></a>  s.add(&#39;b&#39;);
<a name="line443"></a>  s.add(&#39;c&#39;);
<a name="line444"></a>  s.add(&#39;d&#39;);
<a name="line445"></a>  str = &#39;&#39;;
<a name="line446"></a>  goog.structs.forEach(s, function(val, key, set) {
<a name="line447"></a>    assertUndefined(key);
<a name="line448"></a>    assertEquals(s, set);
<a name="line449"></a>    str += val;
<a name="line450"></a>  });
<a name="line451"></a>  assertEquals(str, &#39;abcd&#39;);
<a name="line452"></a>}
<a name="line453"></a>
<a name="line454"></a>
<a name="line455"></a>function testFilter() {
<a name="line456"></a>  var s = new Set;
<a name="line457"></a>  var a = new Number(0); s.add(a);
<a name="line458"></a>  var b = new Number(1); s.add(b);
<a name="line459"></a>  var c = new Number(2); s.add(c);
<a name="line460"></a>  var d = new Number(3); s.add(d);
<a name="line461"></a>
<a name="line462"></a>  var s2 = goog.structs.filter(s, function(val, key, set) {
<a name="line463"></a>    assertUndefined(key);
<a name="line464"></a>    assertEquals(s, set);
<a name="line465"></a>    return val &gt; 1;
<a name="line466"></a>  });
<a name="line467"></a>  assertEquals(stringifySet(s2), &#39;23&#39;);
<a name="line468"></a>
<a name="line469"></a>  s = new Set;
<a name="line470"></a>  s.add(0);
<a name="line471"></a>  s.add(1);
<a name="line472"></a>  s.add(2);
<a name="line473"></a>  s.add(3);
<a name="line474"></a>
<a name="line475"></a>  s2 = goog.structs.filter(s, function(val, key, set) {
<a name="line476"></a>    assertUndefined(key);
<a name="line477"></a>    assertEquals(s, set);
<a name="line478"></a>    return val &gt; 1;
<a name="line479"></a>  });
<a name="line480"></a>  assertEquals(stringifySet(s2), &#39;23&#39;);
<a name="line481"></a>}
<a name="line482"></a>
<a name="line483"></a>
<a name="line484"></a>function testSome() {
<a name="line485"></a>  var s = new Set;
<a name="line486"></a>  var a = new Number(0); s.add(a);
<a name="line487"></a>  var b = new Number(1); s.add(b);
<a name="line488"></a>  var c = new Number(2); s.add(c);
<a name="line489"></a>  var d = new Number(3); s.add(d);
<a name="line490"></a>
<a name="line491"></a>  var b = goog.structs.some(s, function(val, key, s2) {
<a name="line492"></a>    assertUndefined(key);
<a name="line493"></a>    assertEquals(s, s2);
<a name="line494"></a>    return val &gt; 1;
<a name="line495"></a>  });
<a name="line496"></a>  assertTrue(b);
<a name="line497"></a>  var b = goog.structs.some(s, function(val, key, s2) {
<a name="line498"></a>    assertUndefined(key);
<a name="line499"></a>    assertEquals(s, s2);
<a name="line500"></a>    return val &gt; 100;
<a name="line501"></a>  });
<a name="line502"></a>  assertFalse(b);
<a name="line503"></a>
<a name="line504"></a>  s = new Set;
<a name="line505"></a>  s.add(0);
<a name="line506"></a>  s.add(1);
<a name="line507"></a>  s.add(2);
<a name="line508"></a>  s.add(3);
<a name="line509"></a>
<a name="line510"></a>  b = goog.structs.some(s, function(val, key, s2) {
<a name="line511"></a>    assertUndefined(key);
<a name="line512"></a>    assertEquals(s, s2);
<a name="line513"></a>    return val &gt; 1;
<a name="line514"></a>  });
<a name="line515"></a>  assertTrue(b);
<a name="line516"></a>  b = goog.structs.some(s, function(val, key, s2) {
<a name="line517"></a>    assertUndefined(key);
<a name="line518"></a>    assertEquals(s, s2);
<a name="line519"></a>    return val &gt; 100;
<a name="line520"></a>  });
<a name="line521"></a>  assertFalse(b);
<a name="line522"></a>}
<a name="line523"></a>
<a name="line524"></a>
<a name="line525"></a>function testEvery() {
<a name="line526"></a>  var s = new Set;
<a name="line527"></a>  var a = new Number(0); s.add(a);
<a name="line528"></a>  var b = new Number(1); s.add(b);
<a name="line529"></a>  var c = new Number(2); s.add(c);
<a name="line530"></a>  var d = new Number(3); s.add(d);
<a name="line531"></a>
<a name="line532"></a>  var b = goog.structs.every(s, function(val, key, s2) {
<a name="line533"></a>    assertUndefined(key);
<a name="line534"></a>    assertEquals(s, s2);
<a name="line535"></a>    return val &gt;= 0;
<a name="line536"></a>  });
<a name="line537"></a>  assertTrue(b);
<a name="line538"></a>  b = goog.structs.every(s, function(val, key, s2) {
<a name="line539"></a>    assertUndefined(key);
<a name="line540"></a>    assertEquals(s, s2);
<a name="line541"></a>    return val &gt; 1;
<a name="line542"></a>  });
<a name="line543"></a>  assertFalse(b);
<a name="line544"></a>
<a name="line545"></a>  s = new Set;
<a name="line546"></a>  s.add(0);
<a name="line547"></a>  s.add(1);
<a name="line548"></a>  s.add(2);
<a name="line549"></a>  s.add(3);
<a name="line550"></a>
<a name="line551"></a>  b = goog.structs.every(s, function(val, key, s2) {
<a name="line552"></a>    assertUndefined(key);
<a name="line553"></a>    assertEquals(s, s2);
<a name="line554"></a>    return val &gt;= 0;
<a name="line555"></a>  });
<a name="line556"></a>  assertTrue(b);
<a name="line557"></a>  b = goog.structs.every(s, function(val, key, s2) {
<a name="line558"></a>    assertUndefined(key);
<a name="line559"></a>    assertEquals(s, s2);
<a name="line560"></a>    return val &gt; 1;
<a name="line561"></a>  });
<a name="line562"></a>  assertFalse(b);
<a name="line563"></a>}
<a name="line564"></a>
<a name="line565"></a>function testIterator() {
<a name="line566"></a>  var s = new Set;
<a name="line567"></a>  s.add(0);
<a name="line568"></a>  s.add(1);
<a name="line569"></a>  s.add(2);
<a name="line570"></a>  s.add(3);
<a name="line571"></a>  s.add(4);
<a name="line572"></a>
<a name="line573"></a>  assertEquals(&#39;01234&#39;, goog.iter.join(s, &#39;&#39;));
<a name="line574"></a>
<a name="line575"></a>  s.remove(1);
<a name="line576"></a>  s.remove(3);
<a name="line577"></a>
<a name="line578"></a>  assertEquals(&#39;024&#39;, goog.iter.join(s, &#39;&#39;));
<a name="line579"></a>}
</pre>


</body>
</html>
